﻿<script setup lang="ts" name="Drag">
import draggable from 'vuedraggable'
import useScrollPosition from '@/hooks/scrollPosition'
import { storeToRefs } from 'pinia'
import { useSystemConfigStore } from '@/store/storage/systemConfigStore'
import { getIconList } from '@/plugins/svgIcon'
import { cloneDeep } from 'lodash-es'
import { randomColor } from '@/utils/theme'

const route = useRoute()
const systemConfigStore = useSystemConfigStore()
const { systemConfig } = storeToRefs(systemConfigStore)

// 滚动行为
useScrollPosition(route)

const iconList = getIconList()

const iconList1 = ref([])
const iconList2 = ref([])
const iconList3 = ref([])

const getIconListData = () => {
  let resultData = cloneDeep(iconList)
  resultData = resultData.filter((item) => { return item.type === 'ri' })
  iconList1.value = resultData.slice(12 * 3 * 2, 12 * 3 * 2 + 12 * 3)
  iconList2.value = resultData.slice(36 * 4, 36 * 4 + 36)
  iconList3.value = resultData.slice(36 * 6, 36 * 6 + 36)
  iconList1.value.forEach((item) => {
    item.color = randomColor()
  })
  iconList2.value.forEach((item) => {
    item.color = randomColor()
  })
  iconList3.value.forEach((item) => {
    item.color = randomColor()
  })
}

onMounted(() => {
  getIconListData()
})
</script>

<template>
  <el-space :size="systemConfig.space" fill direction="vertical">
    <el-row :gutter="systemConfig.space">
      <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>拖拽排序</span>
            </div>
          </template>
          <draggable class="icon-list" :list="iconList1" :item-key="(key) => key" animation="300">
            <template #item="{ element }">
              <el-col :xs="8" :sm="6" :md="3" :lg="2" :xl="2">
                <el-card shadow="hover" :title="element.key">
                  <svg-icon :icon="element.type + ':' + element.key" size="28" :color="element.color"></svg-icon>
                </el-card>
                <span class="icon-text" :title="element.key">{{ element.key }}</span>
              </el-col>
            </template>
          </draggable>
        </el-card>
      </el-col>
      <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>分组拖拽</span>
            </div>
          </template>
          <el-row :gutter="systemConfig.space">
            <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
              <el-card class="drag-card" shadow="hover">
                <el-scrollbar>
                  <draggable class="icon-list" group="drag" :list="iconList2" :item-key="(key) => key" animation="300">
                    <template #item="{ element }">
                      <el-col :xs="8" :sm="6" :md="6" :lg="4" :xl="4">
                        <el-card shadow="hover" :title="element.key">
                          <svg-icon :icon="element.type + ':' + element.key" size="28" :color="element.color"></svg-icon>
                        </el-card>
                        <span class="icon-text" :title="element.key">{{ element.key }}</span>
                      </el-col>
                    </template>
                  </draggable>
                </el-scrollbar>
              </el-card>
            </el-col>
            <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
              <el-card class="drag-card" shadow="hover">
                <el-scrollbar>
                  <draggable class="icon-list" group="drag" :list="iconList3" :item-key="(key) => key" animation="300">
                    <template #item="{ element }">
                      <el-col :xs="8" :sm="6" :md="6" :lg="4" :xl="4">
                        <el-card shadow="hover" :title="element.key">
                          <svg-icon :icon="element.type + ':' + element.key" size="28" :color="element.color"></svg-icon>
                        </el-card>
                        <span class="icon-text" :title="element.key">{{ element.key }}</span>
                      </el-col>
                    </template>
                  </draggable>
                </el-scrollbar>
              </el-card>
            </el-col>
          </el-row>
        </el-card>
      </el-col>
    </el-row>
  </el-space>
</template>

<style scoped lang="scss">
.el-space {
  width: 100%;
  padding: var(--el-space) var(--el-space) 0;
}

.el-row {
  margin-top: calc(0px - var(--el-space));

  .el-col {
    margin-top: var(--el-space);
  }
}

.icon-list {
  position: relative;
  display: flex;
  flex-wrap: wrap;
  box-sizing: border-box;
  margin: -20px -10px 0;

  .el-col {
    cursor: pointer;

    .el-card {
      :deep(.el-card__body) {
        position: relative;
        display: flex;
        align-items: center;
        justify-content: center;
        height: 70px;
        padding: 15px;
        overflow: hidden;
        transition: all 0.28s;
      }
    }

    .icon-text {
      display: block;
      width: 100%;
      height: 30px;
      overflow: hidden;
      font-size: var(--el-font-size-extra-small);
      line-height: 30px;
      white-space: nowrap;
      text-align: center;
      text-overflow: ellipsis;
    }
  }
}

.drag-card {
  height: 426px;

  :deep(.el-card__body) {
    height: 100%;
    padding: 0;
    overflow: hidden;

    .el-scrollbar {
      .el-scrollbar__wrap {
        overflow: hidden auto;

        .el-scrollbar__view {
          overflow: hidden;
        }
      }
    }
  }

  .icon-list {
    padding: var(--el-space);
  }
}
</style>
